#include "fecudp/udp_endpoint.h"
#include <iostream>

int index = 1;

void messageCallback(const SpUdpEndpoint& endpoint, const asio::ip::udp::endpoint& remote, const char* xxxbuf, size_t xxxlen)
{
    std::string msg(8000, 't');
    char sbuf[200] = {0};
    int slen = sprintf_s(sbuf, 200, "%d", index++);
    memcpy(&msg[0], sbuf, 200);
    endpoint->sendto(msg, "127.0.0.1", 4567);
}

void handleTimter(asio::basic_waitable_timer<std::chrono::system_clock>& looptimer, SpUdpEndpoint client)
{
    looptimer.expires_from_now(std::chrono::milliseconds(100));
    looptimer.async_wait(std::bind(&handleTimter, std::ref(looptimer), client));
    std::string msg(index, 'a' + (index) % 26);
    index++;
    std::cout << "send " << msg[0] << ", size " << msg.size() << std::endl;
    client->sendto(msg, "127.0.0.1", 4567);
}

int main(int argc, char* argv[])
{
    asio::io_service loop;
    SpUdpEndpoint client = std::make_shared<UdpEndpoint>(loop, "0.0.0.0", 4568);
    client->setMessageCallback(messageCallback);
    client->start();
    //client->join_multicast_group("239.255.0.1");
    client->setDecodeBuffer(std::make_shared<MessageDecodeBuffer>(3));
    client->setEncodeBuffer(std::make_shared<MessageEncodeBuffer>(4, 1));
    asio::basic_waitable_timer<std::chrono::system_clock> loop_timer_(loop);

    loop_timer_.expires_from_now(std::chrono::milliseconds(1000));
    loop_timer_.async_wait(std::bind(&handleTimter, std::ref(loop_timer_), client));

    loop.run();

    return 0;
}